<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="kokonut.shop.product.TShirtModel" %>
<%@ page import="kokonut.shop.size.Size" %>
<%@ page import="kokonut.shop.color.Color" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<jsp:useBean id="ProductPersistence" class="persistence.ProductPersistence" scope="request" />
<jsp:useBean id="CartController" class="kokonut.shop.cart.CartController" scope="session" />
<jsp:useBean id="LanguageSelector" class="resources.LanguageSelector" scope="session" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Kokonut Web :: Store</title>
        <style type="text/css" media="all">@import
            "pageStyle.css";</style>
        <link  href="//fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet" type="text/css" />
        <link  href="//fonts.googleapis.com/css?family=Inconsolata:regular" rel="stylesheet" type="text/css" />
        <link  href="//fonts.googleapis.com/css?family=Droid+Sans:regular,bold" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="shop/validateAdding.js"></script>
        <script type="text/javascript">
            var req;
            var isIE;

            function init() {
                colorChooser = document.getElementById("colorChooser");
            }

            function performImageChange(productRef) {
                if(colorChooser.value != "default"){
                    var url = "/changeImage?action=change&ref=" + productRef + "&color=" + escape(colorChooser.value);
                    req = initRequest();
                    req.open("GET", url, true);
                    req.onreadystatechange = callback;
                    req.send(null);
                }
            }

            function callback() {
                //A readyState of "4" signifies the completion of the HTTP interaction
                if (req.readyState == 4) {
                    //200 means "success"
                    if (req.status == 200) {
                        productImage = document.getElementById("productImage");
                        productImage.setAttribute("src", "/serve?blob-key=" + req.responseText);
                    }
                }
            }

            function initRequest() {
                if (window.XMLHttpRequest) {
                    if (navigator.userAgent.indexOf('MSIE') != -1) {
                        isIE = true;
                    }
                    return new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                    isIE = true;
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
        </script>
    </head>
    <body onload="init()">
        <div id="regionSwitcher">
            <fmt:setLocale value="<%=LanguageSelector.getLocaleName()%>" scope="session" />
            <div id="languages">
                <ul>
                    <li id="euskara"><a href="/selectLocale?locale=eu_ES&url=product&ref=<%=request.getParameter("ref")%>"><b>euskara</b></a></li>
                    <li id="castellano"><a href="/selectLocale?locale=es_ES&url=product&ref=<%=request.getParameter("ref")%>"><b>castellano</b></a></li>
                    <li id="english"><a href="/selectLocale?locale=en_UK&url=product&ref=<%=request.getParameter("ref")%>"><b>english</b></a></li>
                </ul>
            </div>
        </div>
        <fmt:bundle basename="ApplicationResources">
            <div id="pageContainer">
                <div id="menu">
                    <ul>
                        <li id="home"><a href="/"><b><fmt:message key="home" /></b></a></li>
                        <li id="store"><a href="/shop"><b><fmt:message key="shop" /></b></a></li>
                        <li id="blog"><a href="/blog"><b><fmt:message key="blog" /></b></a></li>
                        <li id="contact"><a href="/contact"><b><fmt:message key="contact" /></b></a></li>
                    </ul>
                </div>
                <div id="header">
                    <div id="cartPlace"><a href="/cart">
                            <img src="images/Cart.png" alt="cart image">
                            <% if (CartController.getNumberOfProducts() == 0) {
                            %>
                            <fmt:message key="emptyCart" />
                            <%} else {
                            %>
                            <fmt:message key="cart" />: <%=CartController.getNumberOfProducts()%> <fmt:message key="products" />
                            <%}
                            %>
                            <br/>
                            <fmt:message key="totalPrice" />: <%=CartController.getTotalPrice()%> €
                        </a>
                    </div>
                    <h1>KokonutCompany</h1>
               	</div>
               	<div id="sidebar">
               		Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
               	</div>
                <%
                            int ref = Integer.parseInt(request.getParameter("ref"));
                            TShirtModel tShirt = (TShirtModel) ProductPersistence.loadModelByRef(ref);
                %>
                <div id="content">
                    <div class="product">
                        <form name="añadeACarrito" action="/addToCart" method="post">
                            <input id="productName" type="hidden" value="<%=tShirt.getName()%>">
                            <%=tShirt.getName()%><br/>
                            <img id="productImage" alt="" src="<%="/serve?blob-key=" + tShirt.getModelImages().get(0).getImageKey()%>" height="150px" width="180px"/>
                            <select name="colors" id="colorChooser" onchange="performImageChange(<%=tShirt.getRef()%>)">
                                <option value="default"><fmt:message key ="chooseColor"/></option>
                                <%for (Color color : tShirt.getAvailableColors()) {%>
                                <option value="<%=color.getColorName()%>">
                                    <fmt:message key ="<%=color.getColorName()%>"/><%}%></option></select>
                            <select name="sizes"><%for (Size size : tShirt.getAvailableSizes()) {%>
                                <option name="sizes" value="<%=size.getSizeName()%>"><%=size.getSizeName() + " ----  " + tShirt.getPriceBySize(size.getSizeName()) + "€"%><%}%></option></select>
                            <input type="text" name="amount" size="5" value="1">
                            <input type="hidden" name="ref" value="<%=tShirt.getRef()%>"/>
                            <input type="button" value="Add" onclick="validateAdding()"/>
                        </form>
                    </div>
                </div>
                <div id="footer">
                    <div id="altNav"><a href="/"><fmt:message key="home" /></a> | <a href="/shop"><fmt:message key="shop" /></a> | <a href="/blog"><fmt:message key="blog" /></a> | <a href="/contact"><fmt:message key="contact" /></a></div>
                    <div id="communicator"> Tuenti | Facebook | Twitter</div>
                    <div id="admin"><a href="/admin">Admin</a></div>
                </div>
            </div>
        </fmt:bundle>
    </body>
</html>